//
//  ZZScoreLiveController.m
//  NetesaeLottery
//
//  Created by 周英培 on 16/8/6.
//  Copyright © 2016年 周英培. All rights reserved.
//

#import "ZZScoreLiveController.h"
#import "ZZSettingCell.h"

@interface ZZScoreLiveController ()

@property (nonatomic,weak) ZZSettingCell *selectCell;

@property (nonatomic,strong) UITextField *txtField;

@property (nonatomic,strong) UIDatePicker *picker;

@property (nonatomic,strong) UIToolbar *toolbar;

@end

@implementation ZZScoreLiveController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        return;
        
    }
    
    ZZSettingCell *selectCell = [tableView cellForRowAtIndexPath:indexPath];
    
    _selectCell = selectCell;
    
    [selectCell addSubview:self.txtField];
    
    [self.txtField becomeFirstResponder];

}

- (UITextField *)txtField {

    if (!_txtField) {
        
        //MARK:创建文本框
        UITextField *txtField = [[UITextField alloc] init];
        
        // 文本框的输入视图 键盘界面
        txtField.inputView = self.picker;
        
        // 键盘界面上的工具条
        txtField.inputAccessoryView = self.toolbar;
        
        _txtField = txtField;
        
    }
    
    return _txtField;

}

- (UIDatePicker *)picker {

    if (!_picker) {
        
        //MARK:时间选择控件
        UIDatePicker *picker = [[UIDatePicker alloc] init];
        
        // 设置时间显示模式 和当地显示的语言
        picker.datePickerMode = UIDatePickerModeTime;
        picker.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
        
        _picker = picker;
        
    }
    
    return _picker;

}

- (UIToolbar *)toolbar {

    if (!_toolbar) {
        
        //MARK:工具条
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
        
        UIBarButtonItem *cancle = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancleClick)];
        
        UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        
        UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)];
        
        toolbar.items = @[cancle,flex,done];
        
        _toolbar = toolbar;
        
    }
    
    return _toolbar;

}

- (void)cancleClick {

    [self.view endEditing:YES];

}

- (void)doneClick {

    // 1.获取时间数据
    NSDate *time = self.picker.date;
    
    // 2.创建日期格式化对象
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    
    df.dateFormat = @"HH:mm";
    
    // 3.将时间数据转换成字符串
    NSString *timeStr = [df stringFromDate:time];
    
    self.selectCell.timeStr = timeStr;
    
    [self.view endEditing:YES];
    
}


@end
